package main

import (
	"biliMiddleware/core"
	"biliMiddleware/global"
	"biliMiddleware/model"
	"biliMiddleware/rediscli"
	"encoding/json"
	"sync"
	"time"
)

func main() {
	var wg sync.WaitGroup
	wg.Add(3)
	//go UpProxy(&wg)
	//go ServiceTaskList(&wg)
	//go DispatchTask(&wg)
	go GetClientHash()
	wg.Wait()
}

// GetClientHash 定时更新客户端的Hash值
func GetClientHash() {
	for {
		hash := core.ServiceGetProgramHash()
		_ = rediscli.SetClientHash(hash)
		time.Sleep(time.Second * 30)
	}
}

// ServiceTaskList 更新房间列表状态
func ServiceTaskList(_wg *sync.WaitGroup) {
	defer _wg.Done()
	var (
		wg         sync.WaitGroup
		UpTaskList []model.Task
		TaskList   []model.Task
		proxyArr   []string
		count      int
		index      int
	)
	proxyArr = rediscli.GetProxyStr()
	count = len(proxyArr)
	for {
		if count == 0 {
			proxyArr = rediscli.GetProxyStr()
			count = len(proxyArr)
			time.Sleep(time.Second * 1)
			continue
		}
		index = 0
		TaskList = core.ServiceGetTaskList("")
		UpTaskList = []model.Task{}
		for i := range TaskList {
			if index == count {
				proxyArr = rediscli.GetProxyStr()
				count = len(proxyArr)
				index = 0
			}
			item := &TaskList[i]
			wg.Add(1)
			go core.InfoGetH5Room(*item, proxyArr[index], &wg, &UpTaskList)
			index++
		}
		wg.Wait()
		//for i := range UpTaskList {
		//	item := &UpTaskList[i]
		//	wg.Add(1)
		//	go core.ServicePutTask(*item, &wg)
		//}
		bytes, err := json.Marshal(&UpTaskList)
		if err != nil {
			panic(err)
		}
		err = rediscli.SetTaskStatusStr(bytes)
		if err != nil {
			panic(err)
		}
		wg.Wait()
		for i := 0; i < len(UpTaskList); i++ {
			//global.Log.Debugf("func:ServiceTaskList -> UpTaskList:%v\n", UpTaskList[i].RoomID)
		}
		time.Sleep(time.Second * 30)
	}
}

// UpProxy 更新代理
func UpProxy(wg1 *sync.WaitGroup) {
	defer wg1.Done()
	var (
		wg            sync.WaitGroup
		usableProxies = core.GetAnAgent()
		proxyChan     = make(chan string)
	)
	for {
		rediscli.ClearProxyList()
		for i := 0; i < len(usableProxies); i++ {
			wg.Add(1)
			go core.CheckProxy(proxyChan, &wg)
		}
		for _, proxy := range usableProxies {
			proxyChan <- proxy
		}
		wg.Wait()
		global.Log.Debug("func:UpProxy -> wg.Wait()   time.Sleep(time.Minute * 30)\n")
		time.Sleep(time.Minute * 30)
	}
}

// DispatchTask 派发任务
func DispatchTask(_wg *sync.WaitGroup) {
	defer _wg.Done()
	for {
		taskList := core.ServiceGetTaskList("1")
		keys := rediscli.GetClientAllKey()
		keysLen := len(keys)
		n := 0
		var mapStr []map[string]interface{}
		for i := 0; i < keysLen; i++ {
			mapStr = append(mapStr, map[string]interface{}{"durl": "", "count": 0, "room_id": ""})
		}
		if keysLen != 0 {
			for i := range taskList {
				task := taskList[i]
				if *task.TaskStatus == 1 && *task.RoomStatus == 1 && task.Durl != "" {
					for i := 0; i < keysLen; i++ {
						if len(mapStr) != 0 {
							mapStr[i]["durl"] = ""
							mapStr[i]["count"] = 0
							mapStr[i]["room_id"] = ""
						}
					}
					for i := 0; i < int(*task.Count); i++ {
						if n >= keysLen {
							n = 0
						}
						if len(mapStr) != 0 {
							mapStr[n]["durl"] = task.Durl
							mapStr[n]["count"] = mapStr[n]["count"].(int) + 1
							mapStr[n]["room_id"] = task.RoomID
						}
						n++
					}
					for j, m := range mapStr {
						bin, _ := json.Marshal(m)
						rediscli.SetTaskList(keys[j], string(bin))
					}
				}
			}
		}
		time.Sleep(time.Duration(global.Config.Task.TimeOut) * time.Second)
	}
}
